var question_index=1;
var question_count=0;
$(function() {
    question_count=$('#question-count').val();
    $('.cancel').click(function(){
        $('.question-iframe', window.parent.document).animate({top:'100%'},500,function(){
            
        });
        $('.answer-iframe', window.parent.document).animate({top:'100%'},500,function(){
            
        });
        setTimeout(function(){
            $('.zhezhao', window.parent.document).hide();
        },499);
        window.parent.monthQuestionInterVal();
    });
    $('.confirm').click(function(){
        // if($('.daAnActive').length<question_count){
        //     errorAlert('请回答全部问题');
        //     return;
        // }
        
        var last_index_str=[];
        var index_str=[];
        //20191211新需求，答案可多选,选择的index拼凑的规则改变
        $('.daAn').each(function(){
            index_str=[];
            $(this).find('.daAnActive').each(function(){
                index_str.push($(this).attr('data-index'));
            });
            index_str=index_str.join('&');
            last_index_str.push(index_str);
        });

        last_index_str=last_index_str.join(',');

        var load_alert=loadAlert('提交中...');
        $.ajax({
            type: "post",
            url: "Api/Evaluation/submitMonthAnswer",
            data:{
                park_id:$('#park_id').val(),
                month_id:$('#month_id').val(),
                admin_id:$('#admin_id').val(),
                openid:$('#openid').val(),
                choose_index:last_index_str,
                wx_name:$('#wx_name').val(),
                wx_head_img:$('#wx_head_img').val(),
                question_id:$('#question_id').val()
            },
            async: true,
            dataType: 'json',
            success: function(data) {
                closeAlert(load_alert);
                if (data.status) {
                    var t=JSON.parse(data.v);
                    $('.cancel').click();
                    $('.Model1 .model-answer', window.parent.document).attr('data-id',$('#month_id').val());
                    window.parent.showResult(t[0],t[1]);
                } else {
                    errorAlert(data.msg);
                }
            }
        });
    });

    //选择
    $('.daAn li').click(function(){
        if($(this).attr('data-choose')=='0'){
            if($(this).attr('data-answer-index-count')=='1'){
                $(this).parents('ul').find('li').removeClass('daAnActive');
                $(this).parents('ul').find('li').attr('data-choose','0');
            }
            $(this).addClass('daAnActive');
            $(this).attr('data-choose','1');
        }
        else{
            if($(this).attr('data-answer-index-count')!='1'){
                $(this).removeClass('daAnActive');
                $(this).attr('data-choose','0');
            }
        }
    });

    resetPageButton();
})


function toPrev(){
    if(question_index>1){
        var obj=$('.question-active');
        obj.prev().addClass('question-active');
        obj.removeClass('question-active');
        question_index--;
        resetPageButton();
    }
    
}

function toNext(){
    if(question_index<question_count){
        var obj=$('.question-active');
        obj.next().addClass('question-active');
        obj.removeClass('question-active');
        question_index++;
        resetPageButton();
    }
}

function resetPageButton(){
    $('.question-index').html(question_index);
    if(question_index==1){
        $('.page-prev').hide();
    }
    else{
        $('.page-prev').show();
    }
    if(question_index>=question_count){
        $('.page-next').hide();
    }
    else{
        $('.page-next').show();
    }
}